import Query from "./query.js";
const npmBox = new Query({
    query: "",
    changeQuery(query) {
        this.query = query;
        console.log(this);
        this.clear();
        return this.next();
    },
    async request(index) {
        const { hits } = await fetch(
            "https://ofcncog2cu-2.algolianet.com/1/indexes/npm-search/query?x-algolia-agent=Algolia%20for%20JavaScript%20(3.33.0)%3B%20Browser%20(lite)&x-algolia-application-id=OFCNCOG2CU&x-algolia-api-key=f54e21fa3a2a0160595bb058179bfb1e",
            {
                headers: {
                    "content-type": "application/x-www-form-urlencoded",
                },
                referrer: "https://www.jsdelivr.com/",

                body: `{\"params\":\"query= ${this.query}&page=${index}&hitsPerPage=10&attributesToHighlight=%5B%5D&attributesToRetrieve=%5B%22deprecated%22%2C%22description%22%2C%22githubRepo%22%2C%22homepage%22%2C%22keywords%22%2C%22license%22%2C%22name%22%2C%22owner%22%2C%22version%22%5D&analyticsTags=%5B%22jsdelivr%22%5D\"}`,
                method: "POST",
            }
        ).then((res) => res.json());
        return hits;
    },
});
const ghBox = new Query({
    query: "",
    changeQuery(query) {
        this.query = query;
        console.log(this);
        this.clear();
        return this.next();
    },
    async request(index) {
        const hits = await fetch(
            `https://api.github.com/search/repositories?q=${
                this.query
            }&sort=star&page=${index + 1}`
        )
            .then((res) => res.json())
            .then((res) => {
                console.log(res);
                return res.items.map(
                    ({
                        description,
                        forks,
                        license,
                        name,
                        owner: { login },
                    }) => ({
                        description,
                        forks,
                        license,
                        name,
                        owner: { name: login },
                        type: "gh",
                    })
                );
            });
        return hits;
    },
});
export default {
    state: () => ({
        npmBox,
        ghBox,
        selectedPackage: [],
    }),
    mutations: {
        addPackage(item) {
            this.selectedPackage.push(item);
        },
        removePackage(item) {
            if (item.URL)
                this.selectedPackage = this.selectedPackage.filter(
                    (i) => i.URL === item.URL
                );
        },
    },
    actions: {},
};
